﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using log4net;
using System.Windows.Forms;
using System.Data;
using Tireguit.Nmon.Parser.NmonTables;

namespace Tireguit.Nmon.UI.Elements.Logical
{
    public class TreeElement
    {
        protected ILog _log = LogManager.GetLogger(typeof(TreeElement));
        public TreeNode RootElement {get;protected set;}
        public IDbConnection Connection { get; protected set; }
        protected bool quietopened = false;
        public TreeElement(IDbConnection connection)
        {
            Connection = connection;
            RootElement = new TreeNode("Information Système");
            RootElement.Name = "SystInfo";
            TreeNode generalelement = new TreeNode("Informations générales");
            generalelement.Name = "GeneralInfo";
            RootElement.Nodes.Add(generalelement);
            TreeNode diskelement = new TreeNode("Informations sur les disques");
            diskelement.Name = "DiskInfo";
            RootElement.Nodes.Add(diskelement);
            TreeNode detaildisk = new TreeNode("Contenu des disques");
            detaildisk.Name = "DiskDetail";
            RootElement.Nodes.Add(detaildisk);
            TreeNode adapterelement = new TreeNode("Informations sur les adaptateurs");
            adapterelement.Name = "AdaptInfo";
            RootElement.Nodes.Add(adapterelement);
            TreeNode cdeelement = new TreeNode("Résultats des commandes");
            cdeelement.Name = "ResCde";
            AddCmdName(cdeelement);
            RootElement.Nodes.Add(cdeelement);
        }
        protected bool QuietOpen()
        {
            try
            {
                _log.Debug("start QuietOpen");
                if (Connection.State==ConnectionState.Open)
                {
                    return true;
                }
                Connection.Open();
                quietopened = true;
                return true;
            }
            catch (System.Exception ex)
            {
                _log.Error("QuietOpen", ex);
                return false;
            }
            finally
            {
                _log.Debug("stop QuietOpen");
            }
        }
        protected bool QuietClose()
        {
            try
            {
                _log.Debug("start QuietClose");
                if (!quietopened)
                {
                    return true;
                }
                if (Connection.State == ConnectionState.Closed)
                {
                    return true;
                }
                Connection.Close();
                quietopened = false;
                return true;
            }
            catch (System.Exception ex)
            {
                _log.Error("QuietClose", ex);
                return false;
            }
            finally
            {
                _log.Debug("stop QuietClose");
            }

        }
        public DataTable GetResCdes(string cdename)
        {
            IDataReader reader = null;
            IDbCommand command = null;
            try
            {
                _log.Debug("start GetResCdes");
                if (!QuietOpen())
                {
                    return null;
                }
                DataTable datatable = new DataTable();
                datatable.TableName = "Resultat de la commande";
               // datatable.Columns.Add("Resultat", typeof(string));
                command = Connection.CreateCommand();
                command.CommandText = "SELECT [Infoline] as Resultat FROM " 
                    + TablesDictionnary.Instance["BBBP"] +
                    "WHERE [Command]= '"+cdename +"'"+
                    "order by [Numberline] asc";
                reader = command.ExecuteReader();
                datatable.Load(reader);
              /*  while (reader.Read())
                {
                    datatable.Rows.Add(new object[] { reader.GetString(0) });
                }*/
                datatable.AcceptChanges();
               // reader.Close();
                return datatable;

            }
            catch (System.Exception ex)
            {
                _log.Error("GetResCdes", ex);
                return null;
            }
            finally
            {
                try
                {
                    if (reader != null)
                    {
                        reader.Dispose();
                    }
                    if (command != null)
                    {
                        command.Dispose();
                    }
                    QuietClose();
                }
                catch (System.Exception)
                {

                }
                _log.Debug("stop GetResCdes");
            }

        }
        protected void AddCmdName(TreeNode nodeparent)
        {
            IDataReader reader = null;
            IDbCommand command = null;
            try
            {
                _log.Debug("start AddCmdName");
                if (!QuietOpen())
                {
                    return ;
                }

                command = Connection.CreateCommand();
                command.CommandText = "select distinct [Command] FROM "
                    + TablesDictionnary.Instance["BBBP"];
                reader = command.ExecuteReader();
                int i=0;
                while (reader.Read())
                {
                    TreeNode nodechild=new TreeNode(reader.GetString(0));
                    nodechild.Name = "Cde" + i.ToString();
                    nodeparent.Nodes.Add(nodechild);
                    i++;
                }
                reader.Close();
                return ;

            }
            catch (System.Exception ex)
            {
                _log.Error("AddCmdName", ex);
                return ;
            }
            finally
            {
                try
                {
                    if (reader != null)
                    {
                        reader.Dispose();
                    }
                    if (command != null)
                    {
                        command.Dispose();
                    }
                    QuietClose();
                }
                catch (System.Exception)
                {

                }
                _log.Debug("stop AddCmdName");
            }

        }
        public DataTable GetAdapterInformation()
        {
            IDataReader reader = null;
            IDbCommand command = null;
            try
            {
                _log.Debug("start GetAdapterInformation");
                if (!QuietOpen())
                {
                    return null;
                }
                DataTable datatable = new DataTable();
                datatable.TableName = "Détails des adaptateurs";
             /*   datatable.Columns.Add("Numéro", typeof(int));
                datatable.Columns.Add("Nom", typeof(string));
                datatable.Columns.Add("Location", typeof(string));
                datatable.Columns.Add("Description", typeof(string));
                datatable.Columns.Add("Disques attachés", typeof(string));
*/
                command = Connection.CreateCommand();
                command.CommandText = "SELECT [Adapter_number] as Num ,[Name] as Nom ,[Location],[Description],[Disks] as [Disques attachés] FROM " 
                    + TablesDictionnary.Instance["BBBD"]
                    + "order by [Adapter_number]";
                reader = command.ExecuteReader();
                datatable.Load(reader);
                /*
                while (reader.Read())
                {
                    datatable.Rows.Add(new object[] { reader.GetInt32(0),
                    reader.GetString(1),
                    reader.GetString(2),
                    reader.GetString(3),
                    reader.GetString(4)});
                }*/
                datatable.AcceptChanges();
                //reader.Close();
                return datatable;

            }
            catch (System.Exception ex)
            {
                _log.Error("GetAdapterInformation", ex);
                return null;
            }
            finally
            {
                try
                {
                    if (reader != null)
                    {
                        reader.Dispose();
                    }
                    if (command != null)
                    {
                        command.Dispose();
                    }
                    QuietClose();
                }
                catch (System.Exception)
                {

                }
                _log.Debug("stop GetAdapterInformation");
            }

        }
        public DataTable GetDiskDetails()
        {
            IDataReader reader = null;
            IDbCommand command = null;
            try
            {
                _log.Debug("start GetDiskDetails");
                if (!QuietOpen())
                {
                    return null;
                }
                DataTable datatable = new DataTable();
                datatable.TableName = "Détails des disques";
               // datatable.Columns.Add("Détail", typeof(string));
                command = Connection.CreateCommand();
                command.CommandText = "SELECT [diskinfo] as Détail FROM " + TablesDictionnary.Instance["BBBC"] + "order by [numberline] asc";
                reader = command.ExecuteReader();
                datatable.Load(reader);
             /*   while (reader.Read())
                {
                    datatable.Rows.Add(new object[] { reader.GetString(0) });
                }*/
                datatable.AcceptChanges();
              //  reader.Close();
                return datatable;

            }
            catch (System.Exception ex)
            {
                _log.Error("GetDiskDetails", ex);
                return null;
            }
            finally
            {
                try
                {
                    if (reader != null)
                    {
                        reader.Dispose();
                    }
                    if (command != null)
                    {
                        command.Dispose();
                    }
                    QuietClose();
                }
                catch (System.Exception)
                {

                }
                _log.Debug("stop GetDiskDetails");
            }

        }
        public DataTable GetDiskInformation()
        {
            IDataReader reader = null;
            IDbCommand command = null;
            try
            {
                _log.Debug("start GetDiskInformation");
                if (!QuietOpen())
                {
                    return null;
                }
                DataTable datatable = new DataTable();
                datatable.TableName = "Information de disque";
             /*   datatable.Columns.Add("Nom", typeof(string));
                datatable.Columns.Add("Information", typeof(string));
                datatable.Columns.Add("Type", typeof(string));*/
                command = Connection.CreateCommand();
                command.CommandText = "SELECT [diskname] as Nom ,[diskinfo] as  Information ,[disktype] as Type FROM " + TablesDictionnary.Instance["BBBB"] + "WHERE [diskname]!='' order by [diskname] asc";
                reader = command.ExecuteReader();
                datatable.Load(reader);
              /*  while (reader.Read())
                {
                    datatable.Rows.Add(new object[] { reader.GetString(0), reader.GetString(1),reader.GetString(2) });
                }*/
                datatable.AcceptChanges();
               // reader.Close();
                return datatable;

            }
            catch (System.Exception ex)
            {
                _log.Error("GetDiskInformation", ex);
                return null;
            }
            finally
            {
                try
                {
                    if (reader != null)
                    {
                        reader.Dispose();
                    }
                    if (command != null)
                    {
                        command.Dispose();
                    }
                    QuietClose();
                }
                catch (System.Exception)
                {

                }
                _log.Debug("stop GetDiskInformation");
            }

        }
        public DataTable GetGeneralInformation()
        {
            IDataReader reader = null;
            IDbCommand command = null;
            try
            {
                _log.Debug("start GetGeneralInformation");
                if (!QuietOpen())
                {
                    return null;
                }
                DataTable datatable = new DataTable();
                datatable.TableName = "Information Générales";
               /* datatable.Columns.Add("Nom", typeof(string));
                datatable.Columns.Add("Valeur", typeof(string));*/
                command = Connection.CreateCommand();
                command.CommandText = "SELECT [command] as Nom,[value] as Valeur FROM " + TablesDictionnary.Instance["AAA"] + "WHERE [command] not like 'note%' order by [id] asc";
                reader = command.ExecuteReader();
                datatable.Load(reader);
                /*
                while (reader.Read())
                {
                    datatable.Rows.Add(new object[] { reader.GetString(0), reader.GetString(1) });
                }*/
                datatable.AcceptChanges();
                //reader.Close();
                return datatable;

            }
            catch (System.Exception ex)
            {
                _log.Error("GetGeneralInformation", ex);
                return null;
            }
            finally
            {
                try
                {
                    if (reader != null)
                    {
                        reader.Dispose();
                    }
                    if (command!=null)
                    {
                        command.Dispose();
                    }
                    QuietClose();
                }
                catch (System.Exception )
                {
                	
                }
                _log.Debug("stop GetGeneralInformation");
            }

        }
    }
}
